package com.wthink.newMain.settings.data.inner

import android.os.Bundle
import android.os.SystemClock
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.AdapterView
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import com.wthink.hzgj.R
import com.wthink.hzgj.databinding.FragmentDataBinding
import com.wthink.hzgj.databinding.FragmentDataCanBinding
import com.wthink.newMain.settings.data.inner.viewmodel.DataCanViewmodel
import com.wthink.newMain.settings.data.viewmodel.DataViewmodel
import kotlinx.android.synthetic.main.fragment_data_can.*
import kotlinx.android.synthetic.main.fragment_data_port.*

class DataCanFragment : Fragment() {
    private val viewModel by viewModels<DataCanViewmodel>()

    private lateinit var binding: FragmentDataCanBinding

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding = DataBindingUtil.inflate(
            inflater,
            R.layout.fragment_data_can,
            container,
            false
        )
        binding.viewModel = viewModel
        binding.lifecycleOwner = this
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        viewModel.start { baudrateList, serialList ->
            activity?.runOnUiThread {
                serialList.forEachIndexed { index, serialData ->
                    val indexOf = baudrateList.indexOf(serialData.baudRate)
                    when (index) {
                        0 -> {
                            spinner_can1.setSelection(indexOf)
                        }
                        1 -> {
                            spinner_can2.setSelection(indexOf)
                        }
                    }
                }
            }
        }
        binding.spinnerCan1.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
            override fun onNothingSelected(parent: AdapterView<*>?) {

            }

            override fun onItemSelected(
                parent: AdapterView<*>?,
                view: View?,
                position: Int,
                id: Long
            ) {
                viewModel.baudRate1.postValue(viewModel.baudrateList.value!![position])
            }
        }

        binding.spinnerCan2.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
            override fun onNothingSelected(parent: AdapterView<*>?) {

            }

            override fun onItemSelected(
                parent: AdapterView<*>?,
                view: View?,
                position: Int,
                id: Long
            ) {
                viewModel.baudRate2.postValue(viewModel.baudrateList.value!![position])
            }
        }

        binding.commit.setOnClickListener {
            viewModel.config()
        }
    }
}